class Solution:
    def isHappy(self, n: int) -> bool:
        mid_set = set()
        while n != 1:
            if n in mid_set:
                return False
            mid_set.add(n)
            mid_num = 0
            while n != 0:
                mid_num += (n % 10) ** 2
                n //= 10
            n = mid_num
        return True


a = Solution()
print(a.isHappy(19))
